﻿using System;
using System.Collections.Generic;

using LastLand.Data.Global;
using LastLand.Data.Connection;

using Up2Link.SegString;
using Up2Link.Server;

namespace LastLand.Core.MsgProcessor
{
    public class CMsgTalk : IMessageProcessor
    {
        public void ProcessMessage(CMessagePack<CMessageSS> _msgPack)
        {
            Int32 index = _msgPack.index;
            String content = _msgPack.message.MsgContent;
            if (CData.connections[index].IsInGame())
            {
                String account = CData.connections[index].Account;
                Int32 charaIndex = CData.userAccounts[account].ActiveCharacterIndex;
                if (CData.world.IsCharacterDead(charaIndex))
                {
                    CMessageSS characterDeadMsg = new CMessageSS(CConst.SERVER_MESSAGE_CHARACTER_DEAD, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> characterDeadMsgPack = new CMessagePack<CMessageSS>(characterDeadMsg, _msgPack.index);
                    CData.server.Send(characterDeadMsgPack);
                }
                else
                {
                    CMessageSS okTalkMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_TALK, 0, CMessageSS.ENCODING_UTF32, content);
                    CMessagePack<CMessageSS> okTalkMsgPack = new CMessagePack<CMessageSS>(okTalkMsg, index);
                    CData.server.Send(okTalkMsgPack);

                    List<Int32> indexes = CData.world.Talk(charaIndex);
                    foreach (Int32 character in indexes)
                    {
                        foreach (CUserAccount uAccount in CData.userAccounts.Values)
                        {
                            if (uAccount.IsBinded() && uAccount.ActiveCharacterIndex == character)
                            {
                                okTalkMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_TALK, 0, CMessageSS.ENCODING_UTF32, content);
                                okTalkMsgPack = new CMessagePack<CMessageSS>(okTalkMsg, uAccount.ConnectionBinding);
                                CData.server.Send(okTalkMsgPack);
                                break;
                            }
                        }
                    }

                    
                }
            }
            else if (CData.connections[index].IsLoggedIn())
            {
                CMessageSS clientNotPlayingMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_NOT_PLAYING, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> clientNotPlayingMsgPack = new CMessagePack<CMessageSS>(clientNotPlayingMsg, _msgPack.index);
                CData.server.Send(clientNotPlayingMsgPack);
            }
            else
            {
                CMessageSS clientNotLoggedInMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_NOT_LOGGED_IN, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> clientNotLoggedInMsgPack = new CMessagePack<CMessageSS>(clientNotLoggedInMsg, _msgPack.index);
                CData.server.Send(clientNotLoggedInMsgPack);
            }
        }
    }
}
